﻿var common = {
    //验证身份证的合理性
    IdentityCodeValid: function (code) {
        var city = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江 ", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北 ", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏 ", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外 " };
        var tip = "";
        var pass = true;

        if (!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(code)) {
            tip = "身份证号格式错误";
            pass = false;
        }

        else if (!city[code.substr(0, 2)]) {
            tip = "地址编码错误";
            pass = false;
        }
        else {
            //18位身份证需要验证最后一位校验位
            if (code.length == 18) {
                code = code.split('');
                //∑(ai×Wi)(mod 11)
                //加权因子
                var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
                //校验位
                var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
                var sum = 0;
                var ai = 0;
                var wi = 0;
                for (var i = 0; i < 17; i++) {
                    ai = code[i];
                    wi = factor[i];
                    sum += ai * wi;
                }
                var last = parity[sum % 11];
                if (parity[sum % 11] != code[17]) {
                    tip = "校验位错误";
                    pass = false;
                }
            }
        }
        return pass;
    },

    //验证手机号
    CheckMobile: function (mobile) {
        var reg = /^1[0-9]{10}$/;  //电话号码的正则匹配式
        if (reg.test(mobile)) {
            return true;
        }
        return false;
    },

    //js 数组去重
    removeDuplicates: function (arr) {
        var temp = {}, r = [];

        for (var i in arr)

            temp[arr[i]] = true;

        for (var k in temp)

            r.push(k);

        return r;
    },

    //通过name获取复选框选中的值
    //type：endTrim：去除逗号，然后拼接数据 noTrim：不用去除直接拼接数据
    selectvaluebyName: function (name,type) {
        var obj = document.getElementsByName(name);
        var arr = new Array();
        for (var i in obj) {
            if (obj[i].checked)
                if (obj[i].value) {
                    if (type == 'endTrim') {                        
                        arr.push(obj[i].value.substr(0, obj[i].value.length - 1));
                    }
                    else {
                        arr.push(obj[i].value);
                    }
                }                
        }
        var vals = "";
        if (arr.length > 0 && type == 'noTrim') vals = arr.join(",");
        else if (arr.length > 0 && type == 'endTrim') {
            vals = arr.join("");
            vals = vals + ",";
        } 
        return vals;
    },

    //获取复选框选择的数据
    selecttext :function(name){
        var obj = document.getElementsByName(name);
        var arr = new Array();
        for (i in obj) {
            if (obj[i].checked) {
                if (obj[i].nextSibling.textContent)
                    arr.push(obj[i].nextSibling.textContent);
            }
        }
        var texts = arr.join(",");; // 去除最后的逗号
        return texts;
    },
     
    //获取alt的值
    selectCheckBoxAlt: function (name) {
        var obj = document.getElementsByName(name);
        var arr = new Array();
        for (i in obj) {
            if (obj[i].checked) {
                if (obj[i].alt)
                    arr.push(obj[i].alt);
            }
        }
        var texts = arr.join(","); // 去除最后的逗号
        return texts;
    },
    //获取自定义属性的值 hlh 2020-01-10
    selectCheckBoxdata: function (name,definename) {
        var obj = document.getElementsByName(name);
        var arr = new Array();
        for (i in obj) {
            if (obj[i].checked) {
                 
                if (obj[i].getAttribute(definename))
                arr.push(obj[i].getAttribute(definename));
            }
        }
        var texts = arr.join(","); // 去除最后的逗号
        return texts;
    },

    //2019-3-18 hlh 服务端验证身份证的有效性
    validatepid: function (pid,url) {
        var str;
        //验证身份证号正确性
        bui.ajax({
            url: url,
            data: { pid: pid },
            method: "POST",
            async: false,
            dataType: "json"
        }).then(function (res) {
            str =  res.result;           
        }, function (res) {
            bui.alert("验证失败，请联系技术人员");
            
        });
        return str;
    },

    //2019-3-29 hlh 根据传入的时间，计算上一个的同一时间
    getPreMonth: function (date) {
        var arr = date.split('-');
        var year = arr[0]; //获取当前日期的年份
        var month = arr[1]; //获取当前日期的月份
        var day = arr[2]; //获取当前日期的日
        var days = new Date(year, month, 0);
        days = days.getDate(); //获取当前日期中月的天数
        var year2 = year;
        var month2 = parseInt(month) - 1;
        if (month2 == 0) {
            year2 = parseInt(year2) - 1;
            month2 = 12;
        }
        var day2 = day;
        var days2 = new Date(year2, month2, 0);
        days2 = days2.getDate();
        if (day2 > days2) {
            day2 = days2;
        }
        if (month2 < 10) {
            month2 = '0' + month2;
        }
        var t2 = year2 + '-' + month2 + '-' + day2;
        return t2;
    },
    //获取单选按钮的值 2020-01-15
    getradiovalue: function (name) {
        var str = "";
        var radiobox = document.getElementsByName(name);
        for (var i = 0; i < radiobox.length; i++) {
            if (radiobox[i].checked) {
                str = radiobox[i].value;
            }
        }
        return str;
    },
    //设置单选按钮选中，方式一 2020-01-16
    settadioselect: function (name, value) {
        $("input[name='" + name + "'][value='" + value + "']").attr("checked", "checked");
    },
    //设置复选款选中 2020-01-17
    setcheckboxselect: function (name, value) {
        if (value) {
            var arr = value.split(',');
            var checkbox = document.getElementsByName(name);
            for (var i = 0; i < checkbox.length; i++) {
                if (checkbox[i].value) {
                    for (var j = 0; j < arr.length; j++) {
                        if (checkbox[i].value == arr[j]) {
                            checkbox[i].checked = true;
                        }
                    }
                }
            }
        } 
    },
    //生成GUID
    newGuid: function (length) {
        var guid = "";
        for (var i = 1; i <= length; i++) {
            var n = Math.floor(Math.random() * 16.0).toString(16);
            guid += n;
        }
        return guid;
    },
    //数据截取
    SubString: function (code, start, end) {
        var str = "";
        if (code) str = code.substring(start, end);
        return str;
    },
    //如果数据的长度大于一个值，截取前面的数据
    SubStringIf: function (code, start, end, leng) {
        var str = "";
        if (code) {
            if (code.length > leng)
                str = code.substring(start, end);
            else str = code;
        }
        return str;
    },
    //截取字符串的最后一个字符
    SubStringEnd: function (code) {
        var str = "";
        if (code) str = code.substring(0, code.length - 1);
        return str;
    },
    //获取当前年份
    doHandleYear: function () {
        var myDate = new Date();
        var tYear = myDate.getFullYear();

        return tYear;
    },
    //获取当前月份
    doHandleMonth: function () {
        var myDate = new Date();
        var tMonth = myDate.getMonth();

        var m = tMonth + 1;
        if (m.toString().length == 1) {
            m = "0" + m;
        }
        return m;
    },
    //indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。
    //如果要检索的字符串值没有出现，则该方法返回 - 1。
    //出现 ：true 没有出现：false
    indexOfContain: function (strA, strB) {
        var str = false;
        if (!strA || !strB) return false;
         
        if (strA.indexOf(strB) != -1)
            str = true;

        return str;
    },
    //保留两位小数,四舍五入
    toDecimal2: function (num) {
        var f = parseFloat(num);
        if (isNaN(f)) {
            return;
        }
        f = Math.round(num * 100) / 100;
        //console.log(f);
        return f; 
    },
    /*
    * 获得时间差,时间格式为 年-月-日 小时:分钟:秒 或者 年/月/日 小时：分钟：秒
    * 其中，年月日为全格式，例如 ： 2010-10-12 01:00:00
    * 返回精度为：秒，分，小时，天
    */
    GetDateDiff: function (startTime, endTime, diffType) {
        //将xxxx-xx-xx的时间格式，转换为 xxxx/xx/xx的格式
        startTime = startTime.replace(/\-/g, "/");
        endTime = endTime.replace(/\-/g, "/");

        //将计算间隔类性字符转换为小写
        diffType = diffType.toLowerCase();
        var sTime = new Date(startTime);      //开始时间
        var eTime = new Date(endTime);  //结束时间
        //作为除数的数字
        var divNum = 1;
        switch (diffType) {
            case "second":
                divNum = 1000;
                break;
            case "minute":
                divNum = 1000 * 60;
                break;
            case "hour":
                divNum = 1000 * 3600;
                break;
            case "day":
                divNum = 1000 * 3600 * 24;
                break;
            default:
                break;
        }
        return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(divNum));
    }
   
}